
import Mock from 'mockjs'

Mock.mock('/api/user/login', 'post',(options)=>{
  const { username, password} = JSON.parse(options.body)
  if(username === 'admin' && password === '123456'){
    return {
      code: 200,
      message: '登录成功',
      data: {
        token: 'mock-token'
      }
    }
  }else{
    return {
      status: 404,
      message: '用户名或密码错误'
    }
  }
})

Mock.mock('/api/user/info', 'get', {
  code: 200,
  message: '获取用户信息成功',
  data: {
    name: '管理员',
    avatar: 'https://picsum.photos/100/100',
    roles: ['admin']
  }
})

Mock.mock(/\/api\/product\/list/, 'get',(options)=>{
  const { page = 1, size = 10 } = new URLSearchParams(options.url.split('?')[1] || '')
  return  {
    code: 200,
    message: 'success',
    data: Mock.mock({
        'list|10': [{
          'id|+1': (page - 1) * size + 1,
          'name|1': ['@ctitle(5, 10)'],
          'price|100-10000': 1,
          'stock|0-1000': 1,
          'category|1': ['@pick(["AI算力", "智能制造", "新能源汽车", "低空经济"])'],
          'description|1': ['@cparagraph(1, 2)'],
          'createTime': '@datetime(yyyy-MM-dd HH:mm:ss)'
        }]
      }),
    total: 150
  }
})

Mock.mock(/\/api\/product\/detail\/\d+/, 'get', {
  code: 200,
  message: '获取产品详情成功',
  data: {
    'id': 1,
    'name': '@ctitle(5, 10)',
    'price|100-10000': 1,
    'stock|0-1000': 1,
    'category': '@pick(["AI算力", "智能制造", "新能源汽车", "低空经济"])',
    'description': '@cparagraph(1, 3)',
    'createTime': '@datetime',
    'specs': {
      'cpu': '@pick(["Intel Xeon", "AMD EPYC"])',
      'memory': '@pick(["32GB", "64GB", "128GB"])',
      'storage': '@pick(["1TB SSD", "2TB SSD", "4TB HDD"])'
    }
  }
})

Mock.mock(/\/api\/category\/list/, 'get',(options)=>{
  const { page = 1, size = 10 } = new URLSearchParams(options.url.split('?')[1] || '')
  return  {
    code: 200,
    message: 'success',
    data: Mock.mock({
        'list|10': [{
        'id|+1':(page - 1) * size + 1,
        name: '@pick(["智能手机","笔记本","家电","食品","服饰","美妆","运动","图书"])',
        description: '@cparagraph(1)',
        'productCount|50-500': 1,
        createTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
      }]
      }),
    total: 32
  }
})

